package protectjava;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@WebServlet(name = "Refresh")
public class Refresh extends HttpServlet {
//    使用doPost方法请求的方法
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
//使用doGet方法请求的方法
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        设置刷新 自动刷新时间为5秒
        response.setIntHeader("refresh",5);
//        设置响应类型
        response.setContentType("text/html;charset=UTF-8");

//        使用默认时区和语言环境获得一个日历
        Calendar cale = Calendar.getInstance();
//        将Calendar类型转化为 Data类型
        Date tasktime = cale.getTime();
//        设置日期输出的格式
        SimpleDateFormat df = new SimpleDateFormat(
                "yyyy-MM-dd hh:mm:ss");
//        格式化输出
        String nowTime = df.format(tasktime);
        PrintWriter out = response.getWriter();
        String title = "自动刷新 header 设置";
        String doctype =
                "<!DOCTYPE html>\n";
            out.print(doctype +
                    "<html>\n" +
                    "<header><title>" + title + "</title></header>\n" +
                    "<body bgcolor=\"#f0f0f0\">\n" +
                     "<h1 alang = \"cente\">" + title +"</h1>\n" +
                    "<p>当前时间是：" + nowTime + "</p>");
    }
}
